<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
<head>
    <title>AiDex Sharp 快速开发平台-登录页面</title>
    <link rel="stylesheet" type="text/css" href="/static/regist/css/style.css">
    <link rel="icon" href="/static/regist/img/favicon.ico">
    <link href="https://fonts.googleapis.com/css?family=Poppins:600&display=swap" rel="stylesheet">
    <script src="https://kit.fontawesome.com/a81368914c.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

</head>
<body>
<img class="wave" src="/static/regist/img/wave.png">
<div class="container">
    <div class="img">
        <img src="/static/regist/img/bg.svg">
    </div>
    <div class="login-content">
        <form action="/user/register">
            <img style="border: 1px solid #d5d5d5;border-radius: 8px;" src="/static/regist/img/logo.svg">
            <h2 class="title" style="font-size: 36px;">若依-AiDex Sharp</h2>
            <div class="input-div one">
                <div class="i">
                    <i class="fas fa-user"></i>
                </div>
                <div class="div">
                    <h5 id="name_msg">用户名</h5>
                    <input id="name" autofocus name="name" type="text" class="input">
                </div>
            </div>
            <div class="input-div pass">
                <div class="i">
                    <i class="fas fa-lock"></i>
                </div>
                <div class="div">
                    <h5>密码</h5>
                    <input id="pass" name="pass" type="password" class="input">
                </div>
            </div>
            <div class="input-div pass">
                <div class="i">
                    <i class="fas fa-lock"></i>
                </div>
                <div class="div">
                    <h5>确认密码</h5>
                    <input id="repass" type="password" class="input">
                </div>
            </div>
            <input type="submit"  class="btn" value="注册">
        </form>
    </div>
</div>
<script type="text/javascript" src="/static/regist/js/main.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    layui.use(function(){
        var $ = layui.$;
        $("#name").blur(function (){
            $.ajax({
                url:"/user/checkUserNameUnique",//请求地址，请求谁？
                type:"post",
                data:{name:$("#name").val()},
                dataType:"text",
                success:function (data) {
                    if(data == "true"){
                        console.log(data);
                        $("#name_msg").html("");
                        $("input[type=submit]").attr("disabled",false).css("background-color","dodgerblue");
                    } else {
                        $("#name_msg").html("用户名已存在").css("color","red");
                        $("input[type=submit]").attr("disabled",true).css("background-color","#ccc");
                    }
                }
            })
        })
    });

    $("form").submit(function(event){
        event.preventDefault();
        var formData = {
            name: $("#name").val(),
            pass: $("#pass").val()
        };
        $.ajax({
            url: "/user/register",
            type: "post",
            data: JSON.stringify(formData),
            contentType: "application/json",
            dataType: "json",
            success: function(response) {
                // 注册成功后跳转到登录页面
                window.location.href = "/login.html";
            },
            error: function(error) {
                console.error(error);
                alert("注册失败，请重试。");
            }
        });
    });

</script>
<script>
    layui.use(function(){
        var $ = layui.$;
        // 检查用户名是否唯一的代码...
        // 添加检查密码是否一致的函数
        function checkPasswords() {
            var password = $("#pass").val();
            var repassword = $("#repass").val();
            if (password!== repassword) {
                // 如果密码不一致，显示提示信息并禁用提交按钮
                $("h5:last").html("两次输入的密码不同").css("color","red");
                $("input[type=submit]").attr("disabled",true).css("background-color","#ccc");
            } else {
                // 如果密码一致，清除提示信息并启用提交按钮
                $("h5:last").html("");
                $("input[type=submit]").attr("disabled",false).css("background-color","dodgerblue");
            }
        }
        // 监听确认密码输入框的变化事件，触发密码检查
        $("#repass").on("input", checkPasswords);
        $("#pass").on("input", function () {
            if ($("#repass").val() === "") {
                $("h5:last").html("");
            }
        });
    });
</script>
</body>
</html>
